import React, { Component } from 'react';
import './App.css';
import axios from './axios/index'
import PropTypes from 'prop-types';
import {CONFIG} from './config/index'
const OAUTH_URL=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${CONFIG.APP_ID}&redirect_uri=http%3a%2f%2fats8i9cyx2.51http.tech%2foauth&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
class App extends Component {

  static contextTypes = {
    router: PropTypes.object
}
constructor(props){
    super(props);
}
componentWillMount(){
  let publicList = [
    "/oauth"
  ];
  let currPath=this.context.router.history.location.pathname;
  console.log('currPath::',currPath);
  let isHave=publicList.indexOf(currPath);//判断是否包含鉴权路径(<0,不包含)
  if(isHave<0){//判断当前是否是鉴权页面，不是才去鉴权，如果已经是鉴权页面，就去执行鉴权页面的方法存储token
     //开始鉴权 如果未通过则发起微信网页授权
     if(window.localStorage.accessToken==undefined || window.localStorage.accessToken==null || window.localStorage.accessToken==''){
      console.log('开始微信授权，跳转到：'+OAUTH_URL);
      window.location.href=OAUTH_URL;      
    }else{
      
      console.log('您已授权过，可以使用了at:',window.localStorage.accessToken);
      axios.ajax({
        url:'/api/v1/user/info',
        method:'post',
        data:{}
    }).then((res)=>{
        console.log('info:',res);
        if(res.code===0){
            if(res.data!='' || res.data!=null){
                window.localStorage.userInfo=res.data;
                if(window.localStorage.userInfo==undefined || window.localStorage.userInfo==null || window.localStorage.userInfo=='' ){
                  this.context.router.history.push('/login');
                }else{
                  this.context.router.history.push('/main');
                }
            }
        }
        
    });
      
      
    }
  }else{
    console.log('当前路径：',currPath);
  }   
}
  render() {
    return (
      <div>
        {this.props.children}
      </div>
    );
  }
}

export default App;
